// COptionDialog.cpp : Implementierungsdatei
//

#include "stdafx.h"
#include "Charplan.h"
#include "COptionDialog.h"
#include "CharplanView.h"



// COptionDialog-Dialogfeld

IMPLEMENT_DYNAMIC(COptionDialog, CDialog)
COptionDialog::COptionDialog(CWnd* pParent /*=NULL*/)
	: CDialog(COptionDialog::IDD, pParent)
	, mv_save_modified(FALSE)
	, mv_show_skill_points(FALSE)
	, mv_auto_update(FALSE)
{
}

COptionDialog::~COptionDialog()
{
}

void COptionDialog::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    DDX_Check(pDX, IDC_CHECK2, mv_save_modified);
    DDX_Check(pDX, IDC_CHECK5, mv_show_skill_points);
    DDX_Check(pDX, IDC_CHECK6, mv_alternative_skill_display);
    DDX_Control(pDX, IDC_COMBO2, mwnd_update_check_interval);
    DDX_Control(pDX, IDC_AUTO_UPDATE, mwnd_auto_update);
    DDX_Check(pDX, IDC_AUTO_UPDATE, mv_auto_update);
    DDX_Control(pDX, IDC_LIST1, mwnd_tablist);
}


BEGIN_MESSAGE_MAP(COptionDialog, CDialog)
	ON_BN_CLICKED(IDC_BUTTON1, OnBnClickedButton1)
	ON_CBN_SELCHANGE(IDC_COMBO2, &COptionDialog::OnCbnSelchangeCombo2)
    ON_BN_CLICKED(IDC_BUTTON2, &COptionDialog::OnBnClickedButton2)
    ON_BN_CLICKED(IDC_BUTTON3, &COptionDialog::OnBnClickedButton3)
END_MESSAGE_MAP()



//////////////////////////////////////////////////////////////////////////
void	COptionDialog::ShowValues()
{
	mv_save_modified = GETOPTION(_T("SAVE_MODIFIED"));
	mv_show_skill_points = GETOPTION(_T("SHOW_SKILL_POINTS"));
	mv_alternative_skill_display = GETOPTIONDEF(_T("NEW_SKILL_DISPLAY"),1);

	mv_auto_update = GETOPTIONDEF(_T("UPDATE_SILENT"),0);

	int n = GETOPTION(_T("UPDATE_CHECK"))-1;
	if (n<0)
	{
		// unset -> check registry
		n=1;
		CString last_time = AfxGetApp()->GetProfileString(_T("Settings"),_T("lastdate"),_T("1970-11-25 17:04"));
		if (last_time.Left(1)==_T("f") || last_time.Left(1)==_T("n"))	n=0;
	}
	mwnd_update_check_interval.SetCurSel(n);
	if (n==0) mwnd_auto_update.EnableWindow(FALSE);



    // Tabs
    mwnd_tablist.ResetContent();
    mwnd_tablist.SetCheckStyle(BS_AUTOCHECKBOX);


    std::set<DWORD> tab_used;
    for (int t=0;all_tabs[t];t++)   tab_used.insert(all_tabs[t]);

    for (int t=0;all_tabs[t];t++)
    {
        CString str;
        str.Format("TAB%i",t);
        int n = GETOPTION(str);

        // check if valid
        int p = 0;
        for (p=0;all_tabs[p] && all_tabs[p]!=abs(n);p++);
        if (!all_tabs[p]) continue;

        CString temp;
        temp.LoadString(abs(n)); 
        p = mwnd_tablist.InsertString(t,temp);
        mwnd_tablist.SetItemData(p,abs(n));
        mwnd_tablist.SetCheck(p,n>0?BST_CHECKED:BST_UNCHECKED);

        tab_used.erase(abs(n));
    }

    for (std::set<DWORD>::iterator pc = tab_used.begin();pc != tab_used.end(); pc++)
    {
        CString temp;
        temp.LoadString(*pc); 
        int p = mwnd_tablist.InsertString(mwnd_tablist.GetCount(),temp);
        mwnd_tablist.SetItemData(p,*pc);
        mwnd_tablist.SetCheck(p,BST_CHECKED);
    }



  	UpdateData(FALSE);
}


//////////////////////////////////////////////////////////////////////////
void COptionDialog::OnCbnSelchangeCombo2()
{
	int t = mwnd_update_check_interval.GetCurSel();
	mwnd_auto_update.EnableWindow(t!=0); 
}


//////////////////////////////////////////////////////////////////////////
BOOL COptionDialog::OnInitDialog()
{
	CDialog::OnInitDialog();

	ShowValues();

	return TRUE;  
}



#define SETOPTION(x,y)	(((CCharplanApp*)AfxGetApp())->ma_options.SetValueAndAdd(x,y))

void COptionDialog::OnOK()
{
	UpdateData(TRUE);

	SETOPTION(_T("SAVE_MODIFIED"),mv_save_modified);
	SETOPTION(_T("SHOW_SKILL_POINTS"),mv_show_skill_points);
	SETOPTION(_T("UPDATE_SILENT"),mv_auto_update);
	SETOPTION(_T("NEW_SKILL_DISPLAY"),mv_alternative_skill_display);

 	int n = mwnd_update_check_interval.GetCurSel();
	SETOPTION(_T("UPDATE_CHECK"),n+1);


    for (int t=0;t<mwnd_tablist.GetCount();t++)
    {
        CString temp;
        temp.Format(_T("TAB%i"),t);
        SETOPTION(temp, mwnd_tablist.GetCheck(t)==BST_CHECKED? mwnd_tablist.GetItemData(t):-(int)mwnd_tablist.GetItemData(t));
    }

	CDialog::OnOK();
}


void COptionDialog::OnBnClickedButton1()
{
	((CCharplanApp*)AfxGetApp())->OptionsDefault();
	ShowValues();
}



void COptionDialog::OnBnClickedButton2()
{
    int idx = mwnd_tablist.GetCurSel();
    if (idx == CB_ERR)  return;

    if (idx <0) return;

    CString text;
    DWORD   val;
    int    state;
    mwnd_tablist.GetText(idx,text);
    val = mwnd_tablist.GetItemData(idx);
    state = mwnd_tablist.GetCheck(idx);

    mwnd_tablist.DeleteString(idx);

    idx--;
    idx = mwnd_tablist.InsertString(idx,text);
    mwnd_tablist.SetItemData(idx,val);
    mwnd_tablist.SetCheck(idx,state);

    mwnd_tablist.SetCurSel(idx);
}


void COptionDialog::OnBnClickedButton3()
{
    int idx = mwnd_tablist.GetCurSel();
    if (idx == CB_ERR)  return;

    if (idx > mwnd_tablist.GetCount()-2) return;

    CString text;
    DWORD   val;
    int    state;
    mwnd_tablist.GetText(idx,text);
    val = mwnd_tablist.GetItemData(idx);
    state = mwnd_tablist.GetCheck(idx);

    mwnd_tablist.DeleteString(idx);

    idx++;
    idx = mwnd_tablist.InsertString(idx,text);
    mwnd_tablist.SetItemData(idx,val);
    mwnd_tablist.SetCheck(idx,state);

    mwnd_tablist.SetCurSel(idx);
}